package com.example.administrator.waitan.find.event;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.example.administrator.waitan.R;
import com.example.administrator.waitan.tools.ReadTools;

import java.util.List;

/**
 * Created by Administrator on 2016/1/14.
 */
public class EventRecyclerViewAdapter extends RecyclerView.Adapter<EventRecyclerViewAdapter.MyViewHolder>{
    private Context context;
    private List<EventListData> data;
    private RequestQueue queue;
    private LruCache<String, Bitmap> lruCache;
    private ImageLoader imageLoader;
    private ImageLoader.ImageCache imageCache;

    public EventRecyclerViewAdapter(Context context, List<EventListData> data) {
        this.context = context;
        this.data = data;
        init();

    }

    private void init() {
        queue = Volley.newRequestQueue(context);
        lruCache = new LruCache<String, Bitmap>(20);
        imageCache = new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url) {
                return lruCache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
                lruCache.put(url, bitmap);
            }
        };
        //构建imageLoader对象
        imageLoader = new ImageLoader(queue, imageCache);
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_rc_event,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        String imgurl = data.get(position).getU().getHeader().get(0);
        getBitmapCache(imgurl, holder.readTools);

        holder.tv_name.setText(data.get(position).getU().getName());
        holder.tv_time.setText(data.get(position).getPasstime());
        holder.tv_text.setText(data.get(position).getText());
        holder.tv_tag.setText(data.get(position).getTags().get(0).getName());
        if (data.get(position).getTags().size() > 1){
            holder.tv_tag2.setText(data.get(position).getTags().get(1).getName());
        }
        else{
            holder.tv_tag2.setText("");
        }
        if (data.get(position).getTags().size() > 2){
            holder.tv_tag3.setText(data.get(position).getTags().get(2).getName());
        }else {
            holder.tv_tag3.setText("");

        }

    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    public void getBitmapCache(String imgUrl, ImageView imageView) {
        // 构建图片监听器
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView,R.mipmap.mi_icon,R.mipmap.mi_icon);
        // 图片拉取
        imageLoader.get(imgUrl, listener, 300, 300);
    }
    class MyViewHolder extends RecyclerView.ViewHolder{
        private ReadTools readTools;
        private ImageView iv_tag;
        private TextView tv_name, tv_time, tv_text, tv_tag, tv_tag2, tv_tag3;
        public MyViewHolder(View itemView) {
            super(itemView);
            readTools = (ReadTools) itemView.findViewById(R.id.iv_item_text_funny);
            tv_name = (TextView) itemView.findViewById(R.id.tv_item_event_author);
            tv_time = (TextView) itemView.findViewById(R.id.tv_item_event_date);
            tv_text = (TextView) itemView.findViewById(R.id.tv_item_event_title);
            tv_tag = (TextView) itemView.findViewById(R.id.tv_item_event_tag1);
            tv_tag2 = (TextView) itemView.findViewById(R.id.tv_item_event_tag2);
            tv_tag3 = (TextView) itemView.findViewById(R.id.tv_item_event_tag3);
        }
    }
}
